package com.jeequan.jeepay.mch.controller;

//package .controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.jeequan.jeepay.core.entity.PayDictItem;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.PayDictItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
//import .entity.PayDictItem;
//import .service.PayDictItemService;

 /**
 * 数据字典元素表;(pay_dict_item)表控制层
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Api(tags = "数据字典元素表对象功能接口")
@RestController
@RequestMapping("/api/payDictItem")
public class PayDictItemController extends CommonCtrl {
    @Autowired
    private PayDictItemService payDictItemService;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    @ApiOperation("通过ID查询单条数据")
    @GetMapping("{id}")
    public ResponseEntity<PayDictItem> queryById(@PathVariable("id") Integer id){
        return ResponseEntity.ok(payDictItemService.queryById(id));
    }
    
    /** 
     * 分页查询
     *
     * @param payDictItem 筛选条件
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @PostMapping("/paginQuery")
    public ResponseEntity<PageImpl<PayDictItem>> paginQuery(@RequestBody PayDictItem payDictItem){
        PageRequest pageRequest = PageRequest.of(com.jeequan.jeepay.core.constants.CS.DEFALT_PAGE_NUM, com.jeequan.jeepay.core.constants.CS.DEFALT_PAGE_SIZE);
        //1.分页参数
        long current = pageRequest.getPageNumber();
        long size = pageRequest.getPageSize();
        //2.分页查询
        /*把Mybatis的分页对象做封装转换，MP的分页对象上有一些SQL敏感信息，还是通过spring的分页模型来封装数据吧*/
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<PayDictItem> pageResult = payDictItemService.paginQuery(payDictItem, current,size);
        //3. 分页结果组装
        List<PayDictItem> dataList = pageResult.getRecords();
        long total = pageResult.getTotal();
        PageImpl<PayDictItem> retPage = new PageImpl<PayDictItem>(dataList,pageRequest,total);
        return ResponseEntity.ok(retPage);
    }
    
    /** 
     * 新增数据
     *
     * @param payDictItem 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/add")
    public ResponseEntity<PayDictItem> add(@RequestBody PayDictItem payDictItem){
        return ResponseEntity.ok(payDictItemService.insert(payDictItem));
    }
    
    /** 
     * 更新数据
     *
     * @param payDictItem 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PostMapping("/edit")
    public ResponseEntity<PayDictItem> edit(@RequestBody PayDictItem payDictItem){
        return ResponseEntity.ok(payDictItemService.update(payDictItem));
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @GetMapping("/delete")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id){
        return ResponseEntity.ok(payDictItemService.deleteById(id));
    }

     /**
      * 根据groupCode查询列表
      *
      * @param groupCode 筛选条件
      * @return 查询结果
      */
     @ApiOperation("根据groupCode查询字典")
     @GetMapping("/queryByGroup")
     public ResponseEntity<List<Map<String, Object>>> queryByGroup(String groupCode, String itemCodeName){
         //1.组装参数
         PageRequest pageRequest = PageRequest.of(com.jeequan.jeepay.core.constants.CS.DEFALT_PAGE_NUM, com.jeequan.jeepay.core.constants.CS.DEFALT_PAGE_SIZE);
         //1.分页参数
         long current = pageRequest.getPageNumber();
         long size = pageRequest.getPageSize();
         PayDictItem payDictItem = new PayDictItem();
         payDictItem.setGroupCode(groupCode);
         //2.分页查询
         /*把Mybatis的分页对象做封装转换，MP的分页对象上有一些SQL敏感信息，还是通过spring的分页模型来封装数据吧*/
         com.baomidou.mybatisplus.extension.plugins.pagination.Page<PayDictItem> pageResult = payDictItemService.paginQuery(payDictItem, current, size);
         //3. 分页结果组装
         List<PayDictItem> dataList = pageResult.getRecords();
         List<Map<String, Object>> resultList = dataList.stream().map(x -> {
             Map<String, Object> dataMap = new HashMap<>();
             if (StringUtils.isEmpty(itemCodeName)) {
                 dataMap.put("itemCode", x.getItemCode());
             } else {
                 dataMap.put(itemCodeName, x.getItemCode());
             }
             dataMap.put("itemName", x.getItemName());
             return dataMap;
         }).collect(Collectors.toList());
         return ResponseEntity.ok(resultList);
     }
}